package com.ym.medical.service.impl;

import cn.hutool.core.date.DateUtil;
import com.ym.base.enums.FlagEnum;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.DoctorStudioPricingDao;
import com.ym.medical.domain.entity.DoctorServiceItemEntity;
import com.ym.medical.domain.entity.DoctorStudioEntity;
import com.ym.medical.domain.entity.DoctorStudioPricingEntity;
import com.ym.medical.domain.vo.DoctorServiceItemVO;
import com.ym.medical.service.IDoctorServiceItemSV;
import com.ym.medical.service.IDoctorStudioPricingSV;
import com.ym.medical.service.IDoctorStudioSV;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工作室分成Service业务层处理
 *
 * @author ym
 * @date 2020-08-24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DoctorStudioPricingSVImpl extends BaseSVImpl<DoctorStudioPricingEntity, DoctorStudioPricingDao> implements IDoctorStudioPricingSV {

	@Autowired
	private IDoctorStudioSV doctorStudioSV;

	@Autowired
	private IDoctorServiceItemSV doctorServiceItemSV;

	@Override
	public List<DoctorStudioPricingEntity> queryDoctorStudioPricing(Long doctId, boolean all) {
		List<DoctorStudioPricingEntity> doctorStudioPricings = new ArrayList<>();
		// 医生工作室查询
		DoctorStudioEntity doctorStudio = doctorStudioSV.selectOne(DoctorStudioEntity.builder().belongPerson(doctId).build());
		if (doctorStudio != null) {
			// 分成信息查询
			doctorStudioPricings = this.selectList(DoctorStudioPricingEntity.builder().studioId(doctorStudio.getId()).state(FlagEnum.IS.getKey()).build());
		}
		if (all) {
			// 如果是查看当前医生的,添加当前工作室未开通的
			List<DoctorServiceItemEntity> srviceItem = doctorServiceItemSV.getSrviceItem(doctId);
			srviceItem = srviceItem.stream().filter(n -> n.getIsOpen().equals(FlagEnum.IS.getKey())).collect(Collectors.toList());
			if (doctorStudioPricings.isEmpty()) {
				for (DoctorServiceItemEntity doctorServiceItem : srviceItem) {
					DoctorStudioPricingEntity doctorStudioPricing = new DoctorStudioPricingEntity();
					doctorStudioPricing.setServiceId(doctorServiceItem.getSeriLabel());
					doctorStudioPricing.setServiceIdName(doctorServiceItem.getSeriLabelName());
					doctorStudioPricing.setState(FlagEnum.NO.getKey());
					doctorStudioPricings.add(doctorStudioPricing);
				}
			} else {
				for (DoctorServiceItemEntity doctorServiceItem : srviceItem) {
					if (doctorStudioPricings.stream().filter(n -> n.getServiceId().equals(doctorServiceItem.getSeriLabel())).count() == 0) {
						DoctorStudioPricingEntity doctorStudioPricing = new DoctorStudioPricingEntity();
						doctorStudioPricing.setServiceId(doctorServiceItem.getSeriLabel());
						doctorStudioPricing.setServiceIdName(doctorServiceItem.getSeriLabelName());
						doctorStudioPricing.setStudioId(doctorStudio.getId());
						doctorStudioPricing.setState(FlagEnum.NO.getKey());
						doctorStudioPricings.add(doctorStudioPricing);
					}
				}
			}
		}
		return doctorStudioPricings;
	}

	@Override
	public Long saveDoctorStudioPricing(DoctorStudioPricingEntity doctorStudioPricing, Long userId) {
		if (StringUtils.isEmpty(doctorStudioPricing.getId())) {
			//新增
			DoctorStudioEntity doctorStudio = doctorStudioSV.selectOne(DoctorStudioEntity.builder().belongPerson(userId).build());
			doctorStudioPricing.setStudioId(doctorStudio.getId());
			doctorStudioPricing.setValidTime(new Date());
			if (StringUtils.isEmpty(doctorStudioPricing.getState())) {
				//未选择状态，则设置了比例，默认开通
				doctorStudioPricing.setState(FlagEnum.IS.getKey());
			}
			this.insert(doctorStudioPricing);
		} else {
			//修改
			if (!StringUtils.isEmpty(doctorStudioPricing.getState())) {
				//选择状态
				this.updateById(doctorStudioPricing);
			} else {
				//修改比例
				DoctorStudioPricingEntity newDoctorStudioPricing = new DoctorStudioPricingEntity();
				BeanUtils.copyProperties(doctorStudioPricing, newDoctorStudioPricing);
				newDoctorStudioPricing.setValidTime(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(),1)));
				newDoctorStudioPricing.setState(FlagEnum.NO.getKey());
				DoctorStudioPricingEntity oldDoctorStudioPricing = this.selectById(doctorStudioPricing.getId());
				oldDoctorStudioPricing.setInvalidTime(DateUtil.endOfDay(DateUtil.offsetDay(new Date(),1)));
				this.insert(newDoctorStudioPricing);
				this.updateById(oldDoctorStudioPricing);
			}
		}
		return doctorStudioPricing.getId();
	}
}
